<template>
	<!-- 选择地址 -->
	<view class="selectAddress">
		<!-- 搜索地址 -->
		<view class="searchBox">
			<view class="selectAddress-search">
				<view class="selectAddress-search-site">
					<uni-icons type="location" size="24"></uni-icons>
					<text style="padding: 0px 6rpx; font-weight: bold">
						{{ cityName ? cityName : '暂无' }}
					</text>
					<!-- <uni-icons type="down" size="16"></uni-icons> -->
				</view>
				<view class="selectAddress-search-input">
					<uni-search-bar
						radius="5"
						v-model="searchVal"
						placeholder="请输入您要搜索的地址"
						style="flex: 1"
						clearButton="always"
						cancelButton="none"
						@confirm="getSearchAddress"
						@clear="getCancel"
					/>
				</view>
				<view class="selectAddress-search-title" @click="getSearchAddress">搜索</view>
			</view>

			<!-- 定位地址 -->
			<view class="selectAddress-orientation">
				<view class="orientation-site">
					{{ addressInfo ? addressInfo.name : '暂无位置信息' }}
				</view>
				<view class="orientation-icon" @click="getPoiData">
					<uni-icons type="loop" size="20" color="red" style="margin-right: 6rpx"></uni-icons>
					<text>重新定位</text>
				</view>
			</view>
		</view>

		<!-- 附近地址 -->
		<view class="selectAddress-nearby">
			<view class="selectAddress-nearby-header">
				<uni-icons type="location" size="20" style="margin-right: 6rpx"></uni-icons>
				<text>附近地址</text>
			</view>
			<view class="selectAddress-nearby-list">
				<view class="selectAddress-nearby-item" v-for="item in markersData" :key="item.id" @click="changeAddress(item)">
					{{ item.name }}
				</view>
			</view>
		</view>

		<view class="zw" style="width: 100%; height: 200rpx"></view>

		<!-- 添加新地址 -->
		<view class="addAddressBtn" :style="bottomStyle">
			<u-button type="primary" color="red" text="新增收获地址" shape="circle" @click="gotoAddress"></u-button>
		</view>
	</view>
</template>

<script>
import amapFile from '../../utils/amap-wx.130.js';
export default {
	data() {
		return {
			// 附近地址
			markersData: [],
			searchVal: '',
			// 当前地址信息
			addressInfo: null,
			cityName: ''
		};
	},
	computed: {
		bottomStyle() {
			const systemInfo = uni.getSystemInfoSync();
			const { safeAreaInsets } = systemInfo;
			const bottomSafeAreaHeight = safeAreaInsets.bottom || 0;
			return `padding-bottom: ${bottomSafeAreaHeight ? bottomSafeAreaHeight + 'px' : '20rpx'}`;
		}
	},
	onLoad() {
		this.getPoiData();
	},
	methods: {
		// 搜索地址
		getSearchAddress() {
			this.markersData = [];
			var myAmapFun = new amapFile.AMapWX({ key: '858bf93b69c073e6d292c9f328ad0c80' });
			myAmapFun.getInputtips({
				keywords: this.searchVal,
				citylimit: true,
				city: this.cityName,
				location: this.addressInfo.location,
				success: (data) => {
					if (data && data.tips) {
						this.markersData = data.tips;
						this.addressInfo = this.markersData[0];
					}
				}
			});
		},

		// 取消搜索
		getCancel() {
			this.searchVal = '';
			this.getPoiData();
		},

		gotoAddress() {
			uni.navigateTo({
				url: '/pages/addAddress/addAddress'
			});
		},

		// 获取附近地址信息
		getPoiData() {
			var that = this;
			var myAmapFun = new amapFile.AMapWX({ key: '858bf93b69c073e6d292c9f328ad0c80' });
			that.amapFileInfo = myAmapFun;
			myAmapFun.getPoiAround({
				success: function (data) {
					that.markersData = data.poisData;
					that.addressInfo = that.markersData[0];
					that.cityName = that.markersData[0].cityname;
				},
				fail: function (info) {
					uni.showModal({ title: info.errMsg });
				}
			});
		},

		// 切换地址
		changeAddress(val) {
			uni.showModal({
				title: '提示',
				content: '是否要切换当前地址',
				success: (res)=> {
					if (res.confirm) {
						this.addressInfo = val;
						this.$store.dispatch('getUserAddressInfoAsync',val);
						uni.navigateBack(-1);
					} else if (res.cancel) {
					}
				}
			});
			
		}
	}
};
</script>

<style>
page {
	height: 100%;
	display: flex;
	flex-direction: column;
	background-color: #fff;
}
</style>

<style scoped lang="scss">
.selectAddress {
	flex: 1;
	overflow: scroll;
	display: flex;
	flex-direction: column;

	.searchBox {
		height: 200rpx;
		position: sticky;
		top: 0px;
		left: 0px;
		width: 100%;
		z-index: 99;
		background-color: #fff;
		.selectAddress-search {
			display: flex;
			align-items: center;
			box-sizing: border-box;
			height: 100rpx;
			box-sizing: border-box;
			padding: 0px 20rpx;
			.selectAddress-search-site {
				display: flex;
				align-items: center;
				width: 180rpx;
				font-size: 26rpx;
			}

			.selectAddress-search-input {
				flex: 1;
			}

			.selectAddress-search-title {
				font-size: 28rpx;
			}
		}

		.selectAddress-orientation {
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			padding: 0rpx 20rpx;
			height: 100rpx;
			border-bottom: 1px #f3f3f3 solid;
			font-size: 26rpx;
			.orientation-icon {
				display: flex;
				align-items: center;
				color: red;
			}
			.orientation-site {
				flex: 1;
			}
		}
	}

	.selectAddress-nearby {
		flex: 1;
		overflow: scroll;
		display: flex;
		flex-direction: column;
		.selectAddress-nearby-header {
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 0rpx 20rpx;
			height: 80rpx;
			border-bottom: 1px #f3f3f3 solid;
			font-weight: bold;
			font-size: 28rpx;
		}

		.selectAddress-nearby-list {
			flex: 1;
			overflow: scroll;
			.selectAddress-nearby-item {
				height: 80rpx;
				line-height: 80rpx;
				padding: 0rpx 20rpx;
				border-bottom: 1px #f3f3f3 solid;
				font-size: 26rpx;
			}
		}
	}

	.addAddressBtn {
		position: fixed;
		bottom: 0rpx;
		left: 0px;
		width: 100%;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 0px 20rpx;
		padding-top: 20rpx;
		background-color: #fff;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	}
}
</style>
